//
//  ZLWReadingController.m
//  EPA
//
//  Created by lanou3g on 16/4/25.
//  Copyright © 2016年 zlw. All rights reserved.
//

#import "ZLWReadingController.h"
#import "ZLWUserInfoManager.h"
#import "ZLWCommentController.h"
#import "ZLW_DBManager.h"
@interface ZLWReadingController ()<UIWebViewDelegate>

@property (nonatomic, strong)NSString *html;

@property (nonatomic, strong) NSString *contentid;
@end

@implementation ZLWReadingController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (self.getmdoel.htmlString) {
        UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
        webView.delegate = self;
        [self.view addSubview:webView];
        [webView loadHTMLString:self.getmdoel.htmlString baseURL:nil];
    }else
    {
        [self reloadNetWorking];
    }
    

    
    //先判断是否收藏过
    NSString *collectionBtnTitle = nil;
    if ([[ZLW_DBManager shareMesager]HasCollect:(NSString *)self.getmdoel.ID]) {
        collectionBtnTitle = @"取消收藏";
    }else
    {
        collectionBtnTitle = @"收藏";
    }
    
    UIBarButtonItem *collectBtn = [[UIBarButtonItem alloc] initWithTitle: collectionBtnTitle style:(UIBarButtonItemStylePlain) target:self action:@selector(collectBtnAction:)];
    
    UIBarButtonItem *shareBtn = [[UIBarButtonItem alloc] initWithTitle:@"分享" style:(UIBarButtonItemStylePlain) target:self action:@selector(shareBtnAction:)];
    UIBarButtonItem *commentBtn = [[UIBarButtonItem alloc] initWithTitle:@"评论" style:(UIBarButtonItemStylePlain) target:self action:@selector(commentsBtnAction:)];
    self.navigationItem.rightBarButtonItems = @[collectBtn,commentBtn,shareBtn];
    
    // Do any additional setup after loading the view.
}
-(void)reloadNetWorking
{
    UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:webView];
    webView.delegate = self;
    
    [ZLW_NetWorkRequestManager requestURL:[NSURL URLWithString:READCONTENT_URL ] Method:CK_POST Params:@{@"contentid":self.getmdoel.ID,@"client":@"1",@"devcieid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"auth": [ZLWUserInfoManager shareManager].auth,@"version":@"3.0.2" } Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
            NSDictionary *dict = jsonDict[@"data"];
            self.contentid = dict[@"contentid"];
            NSArray *allVales = [dict allKeys];
            NSLog(@"%@",allVales);
            dispatch_async(dispatch_get_main_queue(), ^{
                [webView loadHTMLString:dict[@"html"] baseURL:nil];
                self.html = dict[@"html"];
               
             
            });
        }
        
    }];
   
}
//收藏
- (void)collectBtnAction:(UIBarButtonItem *)sender
{
    if ([sender.title isEqualToString:@"收藏"]) {
        [sender setTitle:@"取消收藏"];
        [[ZLW_DBManager shareMesager] collectDetailModel:self.getmdoel html:self.html];
    }else
    {
        [sender setTitle:@"收藏"];
        [[ZLW_DBManager shareMesager] CancelCollectArticle:self.getmdoel.ID];
    }
    
}

//评论
- (void)commentsBtnAction:(UIBarButtonItem *)sender
{
    ZLWCommentController *CommentVC = [[ZLWCommentController alloc] init];
    CommentVC.contentid = self.contentid;
    [self.navigationController pushViewController:CommentVC animated:YES];

}

//分享
- (void)shareBtnAction:(UIBarButtonItem *)sender
{
    
}

//将图片大小弄成固定的
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [webView stringByEvaluatingJavaScriptFromString:
     @"var script = document.createElement('script');"
     "script.type = 'text/javascript';"
     "script.text = \"function ResizeImages() { "
     "var myimg,oldwidth;"
     "var maxwidth=380;" //缩放系数
     "for(i=0;i <document.images.length;i++){"
     "myimg = document.images[i];"
     "if(myimg.width > maxwidth){"
     "oldwidth = myimg.width;"
     "myimg.width = maxwidth;"
     "myimg.height = myimg.height * (maxwidth/oldwidth);"
     "}"
     "}"
     "}\";"
     "document.getElementsByTagName('head')[0].appendChild(script);"];
    
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
